//
//  LoginView.m
//  WoChuangFu
//
//  Created by 郑渊文 on 14/12/31.
//  Copyright (c) 2014年 asiainfo-linkage. All rights reserved.
//

#import "LoginView.h"

#define TOP_VIEW_HEIGHT 100
#define INPUT_VIEW_HEIGHT 100
#define BUTTON_HEIGHT  44
#define MARGIN 20

@implementation LoginView

@synthesize SendDic;
@synthesize NameTextField;
@synthesize PassTextField;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [ComponentsFactory createColorByHex:@"#eeeeee"];
//        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignAllFirstResponder)];
//        [self addGestureRecognizer:tapGestureRecognizer];
        [self LayoutV];
    }
    return self;
}

-(void)LayoutV
{

    
    UIImageView *DottedLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_login_xline"]];
    DottedLine.frame = CGRectMake(30,15,250, 44);
    //    [topView addSubview:DottedLine];
    
    UIImageView *banner = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64,self.frame.size.width,TOP_VIEW_HEIGHT)];
    //    banner.backgroundColor = [UIColor redColor];
    banner.image = [UIImage imageNamed:@"banner.jpg"];
    [self addSubview:banner];
    
    UIView *inputBgView = [[UIView alloc] initWithFrame:CGRectMake(0,180,self.frame.size.width,INPUT_VIEW_HEIGHT)];
    [inputBgView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:inputBgView];
    
    UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(50,50, self.frame.size.width,1)];
//    separatorLine.backgroundColor = [ComponentsFactory createColorByHex:@"#eeeeee"];
    [inputBgView addSubview:separatorLine];
    
    NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(55,10,200, 33)];
    NameTextField.placeholder = @"手机号码/QQ/邮箱";
    NameTextField.delegate = self;
    NameTextField.returnKeyType = UIReturnKeyDone;
    NameTextField.keyboardType  =  UIKeyboardTypeNumbersAndPunctuation;
//    NameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [inputBgView addSubview:NameTextField];
    
    PassTextField = [[UITextField alloc] initWithFrame:CGRectMake(55,60,200,33)];
    PassTextField.secureTextEntry =YES;
    PassTextField.placeholder = @"请输入密码";
    PassTextField.delegate = self;
    PassTextField.returnKeyType = UIReturnKeyDone;
//    PassTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [inputBgView addSubview:PassTextField];
    
    UIImageView *userNameIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_login_account"]];
    userNameIcon.contentMode = UIViewContentModeCenter;
    userNameIcon.frame =  CGRectMake(0,0,50,50);
    UIImageView *passWordIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_login_password"]];
    passWordIcon.contentMode = UIViewContentModeCenter;
    passWordIcon.frame =  CGRectMake(0,50,50,50);
    
    [inputBgView addSubview:userNameIcon];
    [inputBgView addSubview:passWordIcon];
    
    
    UIButton *loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame=CGRectMake(80,300,self.frame.size.width-160,44);
    loginBtn.layer.cornerRadius = 5;

    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor = [UIColor orangeColor];
    [self addSubview:loginBtn];
    
    NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
    NSString *passwd = [[NSUserDefaults standardUserDefaults]objectForKey:@"passwd"];
    if (name !=nil &&name.length >0)NameTextField.text = name;
    if (passwd !=nil &&passwd.length >0)PassTextField.text = passwd;
    
    
   }



-(void)login:(UIButton *)sender
{
    if([NameTextField.text length]==0){
        [self showSimpleAlertView:@"请输入账户"];
        [NameTextField becomeFirstResponder];
    }else if([PassTextField.text length]==0){
        [self showSimpleAlertView:@"请输入密码"];
        [PassTextField becomeFirstResponder];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:NameTextField.text forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] setObject:PassTextField.text forKey:@"passwd"];
        if (self.delegate !=nil&&[self.delegate respondsToSelector:@selector(didClickedLogin)]) {
            [self.delegate didClickedLogin];
        }
        
    }
}




#pragma mark -
#pragma mark AlertView
-(void)showAlertViewTitle:(NSString*)title message:(NSString*)message delegate:(id)delegate tag:(NSInteger)tag cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString*)otherButtonTitles,...
{
    NSMutableArray* argsArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    id arg;
    va_list argList;
    if(nil != otherButtonTitles){
        va_start(argList, otherButtonTitles);
        while ((arg = va_arg(argList,id))) {
            [argsArray addObject:arg];
        }
        va_end(argList);
    }
    
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:delegate
                                          cancelButtonTitle:cancelButtonTitle
                                          otherButtonTitles:otherButtonTitles,nil];
    alert.tag = tag;
    for(int i = 0; i < [argsArray count]; i++){
        [alert addButtonWithTitle:[argsArray objectAtIndex:i]];
    }
    [alert show];
    //    [alert release];
    //
    //    [argsArray release];
}


-(void)showSimpleAlertView:(NSString*)message
{
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
    //    [alert release];
}

#pragma mark textFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
}
@end
